﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Diagnostics;
using System.Threading;

namespace Updater
{
    public partial class Updater : Form
    {
        Queue<string> list = new Queue<string>();
        Queue<string> name = new Queue<string>();

        public Updater()
        {
            InitializeComponent();
        }

        public void update()
        {
            WebClient clientWeb = new WebClient();
            Uri url = new Uri("http://homebrewsdmanager.googlecode.com/svn/trunk/update/" + list.Dequeue());
            clientWeb.DownloadFileAsync(url, name.Dequeue());
            clientWeb.DownloadFileCompleted += new AsyncCompletedEventHandler(clientWeb_DownloadFileCompleted);
            clientWeb.DownloadProgressChanged += new DownloadProgressChangedEventHandler(clientWeb_DownloadProgressChanged);
        }

        void clientWeb_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            label1.Text = "Téléchargement de la mise à jour : " + e.ProgressPercentage + "%";
            progressBar1.Value = e.ProgressPercentage;
        }

        void clientWeb_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
        {
            if (list.Count != 0)
            {
                update();
            }
            else
            {
                Process.Start("homebrewsdmanager.exe");
                Application.Exit();
            }
        }

        private void Updater_Load(object sender, EventArgs e)
        {
            File.Delete("homebrewsdmanager.exe");
            File.Delete("thehdownloader.exe");
            list.Enqueue("HSDM.exe");
            list.Enqueue("THD.exe");
            list.Enqueue("Ionic.Zip.dll");
            name.Enqueue("homebrewsdmanager.exe");
            name.Enqueue("thehdownloader.exe");
            name.Enqueue("Ionic.Zip.dll");
            update();
        }
    }
}
